package com.lqm.dao;

import com.lqm.entity.home.HomeUser;
import com.lqm.entity.home.Project;
import com.lqm.entity.home.ProjectType;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ProjectDao  extends JpaRepository<Project,Integer>, PagingAndSortingRepository<Project,Integer> {

    /**
     * 改变项目的状态
     * @param project
     * @param status
     * @return
     */
    @Modifying
    @Query(value = "update Project p set p.status=:status where p.projectId= :#{#project.projectId}")
    Integer setProjectStatus(@Param("project") Project project, @Param("status")int status);

    Project findByProjectId(Integer id);

    /**
     * 通过项目名模糊查询项目，为空查询所有
     * @param status
     * @param name
     * @param pageable
     * @return
     */
    List<Project> findAllByStatusAndNameLike(int status, String name, Pageable pageable);

    /**
     * 总条数，获取分页(通过项目名模糊查询项目，为空查询所有)
     * @param status
     * @param name
     * @return
     */
    Integer countAllByStatusAndNameLike(int status, String name);


    List<Project> findAllByStatusAndProjectTypeAndNameLike(int status, ProjectType projectType,String name, Pageable pageable);
    List<Project> findAllByProjectTypeAndNameLike( ProjectType projectType,String name, Pageable pageable);

    Integer countAllByStatusAndProjectTypeAndNameLike(int status, ProjectType projectType,String name);
    Integer countAllByProjectTypeAndNameLike( ProjectType projectType,String name);

    List<Project> findAllByStatusAndProjectStatus(int status,int projectStatus, Pageable pageable);

    List<Project> findByStatusAndHomeUser(int status, HomeUser user, Pageable pageable);

    @Query("select count (p.projectId) from Project p where p.homeUser=:homeUser and p.status=1")
    Integer getUserProjectCount(@Param("homeUser") HomeUser homeUser);

    Integer countAllByNameLike(String s);

    List<Project> findAllByNameLike(String s, Pageable pageable);

//    Integer deleteByHomeUserAndProjectId(HomeUser homeUser,Integer id);

}
